package com.study.component;

import cn.hutool.core.util.RandomUtil;
import com.study.dto.SubDTO;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author zhuzi
 * 2024/3/26 18:00
 * 队列
 */
@Slf4j
//@Component
public class QueueComponent implements ApplicationRunner, Ordered {
    public static final String queueName = "redissonBasicQueue";
    static RQueue<SubDTO> queue;
    @Resource
    RedissonClient redissonClient;

    @PostConstruct
    public void init() {
        queue = redissonClient.getQueue(queueName);
    }


    /**
     * 每五秒执行一次
     */
    @Scheduled(cron = "*/50 * * * * ?")
    private void pubData() {
        RQueue<SubDTO> queue = redissonClient.getQueue(queueName);
        SubDTO<Object> subDTO = new SubDTO<>().setType(RandomUtil.randomString(5)).setData("测试数据" + RandomUtil.randomString(5)).setPubDateTime(LocalDateTime.now());
        queue.add(subDTO);
        System.out.println("发布消息 :" + subDTO);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
       /* while (true) {
            SubDTO msgData = queue.poll();
            if (Objects.nonNull(msgData)) {
                System.out.println("接收消息A:" + msgData);
            }
        }*/
    }

    @Override
    public int getOrder() {
        //项目启动后执行
        return -1;
    }
}
